<?php

/**
 * This is the model class for table "dict_battery".
 *
 * The followings are the available columns in table 'dict_battery':
 * @property integer $RECORD_ID
 * @property integer $BATTERYSORT_V
 * @property integer $MANCODE_V
 * @property string $BATTERYTYPE_V
 * @property string $VOLTAGE_V
 * @property string $STDINNER_V
 * @property string $STDVOLUME_V
 * @property string $CREATE_DATE
 * @property string $CHARGEVOL_V
 * @property string $B0
 * @property string $B1
 * @property string $YELLOWVALUE_N
 * @property string $REDVALUE_N
 * @property string $REMARK_V
 */
class DictBattery extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return DictBattery the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'dict_battery';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('BATTERYSORT_V,MANCODE_V,BATTERYTYPE_V,CREATE_DATE,STDINNER_V, YELLOWVALUE_N, REDVALUE_N','required'),
			array('BATTERYTYPE_V', 'unique', 'message' => '已存在该电池规格，不允许重复创建'),
			array('BATTERYSORT_V, MANCODE_V', 'numerical', 'integerOnly'=>true),
			array('BATTERYTYPE_V, VOLTAGE_V, STDINNER_V, STDVOLUME_V, CHARGEVOL_V', 'length', 'max'=>50),
			array('B0, B1, YELLOWVALUE_N, REDVALUE_N', 'length', 'max'=>20),
			array('REMARK_V', 'length', 'max'=>255),
			array('CREATE_DATE', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('RECORD_ID, BATTERYSORT_V, MANCODE_V, BATTERYTYPE_V, VOLTAGE_V, STDINNER_V, STDVOLUME_V, CREATE_DATE, CHARGEVOL_V, B0, B1, ISINNER, YELLOWVALUE_N, REDVALUE_N, REMARK_V', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'BATTERYSORT'=>array(self::BELONGS_TO, 'DictBatterySort', 'BATTERYSORT_V'),
			'MANUFACTURER'=>array(self::BELONGS_TO, 'DictManufacturer', 'MANCODE_V'),			
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'RECORD_ID' => '流水',
			'BATTERYSORT_V' => '电池类型',
			'MANCODE_V' => '制造商',
			'BATTERYTYPE_V' => '电池规格',
			'VOLTAGE_V' => '电压(V)',
			'STDINNER_V' => '标称内阻(mΩ)',
			'STDVOLUME_V' => '标称容量(Ah)',
			'CREATE_DATE' => '电池年代',
			'CHARGEVOL_V' => '浮充电压(V)',
			'B0' => 'B0',
			'B1' => 'B1',
			'ISINNER' => '标称计量单位',
			'YELLOWVALUE_N' => '黄色经验值(mΩ)',
			'REDVALUE_N' => '红色经验值(mΩ)',
			'REMARK_V' => '备注',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		if($this->MANCODE_V != ''){
			$manu = DictManufacturer::model()->find("MANNAME_V=:NAME",array(":NAME"=>$this->MANCODE_V));
			if(!empty($manu)){
			$criteria->compare('MANCODE_V', $manu->RECORD_ID);
			}else{
				$criteria->compare('MANCODE_V', 'not exsits');
			}
		}
		
		if($this->BATTERYSORT_V != ''){
			$sort = DictBatterySort::model()->find("SORTNAME_V=:NAME",array(":NAME"=>$this->BATTERYSORT_V));
			if(!empty($sort)){
				$criteria->compare('BATTERYSORT_V', $sort->RECORD_ID);
			}else{
				$criteria->compare('BATTERYSORT_V','no exsits');
			}
		}
		$criteria->compare('RECORD_ID',$this->RECORD_ID);
		
		$criteria->compare('BATTERYTYPE_V',$this->BATTERYTYPE_V,true);
		$criteria->compare('VOLTAGE_V',$this->VOLTAGE_V,true);
		$criteria->compare('STDINNER_V',$this->STDINNER_V,true);
		$criteria->compare('STDVOLUME_V',$this->STDVOLUME_V,true);
		$criteria->compare('CREATE_DATE',$this->CREATE_DATE,true);
		$criteria->compare('CHARGEVOL_V',$this->CHARGEVOL_V,true);
		$criteria->compare('B0',$this->B0,true);
		$criteria->compare('B1',$this->B1,true);
		$criteria->compare('YELLOWVALUE_N',$this->YELLOWVALUE_N,true);
		$criteria->compare('REDVALUE_N',$this->REDVALUE_N,true);
		$criteria->compare('REMARK_V',$this->REMARK_V,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
}